package com.caine.androidtask.ls12;

import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.caine.androidtask.R;
import com.caine.androidtask.launcher.Utils;

public class SensorActivity extends Activity implements SensorEventListener {

	public SensorManager mSensor;
	public Sensor proximitySensor;
	public Sensor gSensor;
	float[] values;

	private TextView sensorInfo;
	private TextView gSensorInfo;
	private ImageView images;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.caine_ls12);
		sensorInfo = (TextView) findViewById(R.id.ls12_sensorlist);
		if (mSensor == null)
			sensorInit();
		List<Sensor> allSensor = mSensor.getSensorList(Sensor.TYPE_ALL);
		String sensor_info = "Sensor info : \n";
		for (Sensor sensor : allSensor) {
			if (Utils.DEBUG)
				Log.e(Utils.TAG, sensor.getName());
			sensor_info += "\t[" + sensor.getName() + "]\n";
		}
		sensorInfo.setText(sensor_info);
		gSensorInfo = (TextView) findViewById(R.id.ls12_gsensorinfo);
		//images = (ImageView) findViewById(R.id.ls12_gsensor);
		
		// lSensor.
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		sensorInit();
		super.onResume();
	}

	// Event
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (proximitySensor != null)
			if (Utils.DEBUG)
				Utils.showMsg(SensorActivity.this,
						proximitySensor.getResolution() + "proximitySensor!");
		if (gSensor != null) {
			values = event.values;
			float x = event.values[SensorManager.DATA_X];
			float y = event.values[SensorManager.DATA_Y];
			float z = event.values[SensorManager.DATA_Z];
			/**double dx = Math.abs(9.8-Math.abs(x));
	    	double dy = Math.abs(9.8-Math.abs(y));
	    	double dz = Math.abs(9.8-Math.abs(z));*/
			showGSensorInfo(x,y,z);
			showArrow(x, y);
		}
	}

	private void sensorInit() {
		if (mSensor == null)
			mSensor = (SensorManager) getSystemService(SENSOR_SERVICE);
		if (proximitySensor == null)
			proximitySensor = mSensor.getDefaultSensor(Sensor.TYPE_PROXIMITY);
		mSensor.registerListener(this, proximitySensor,
				SensorManager.SENSOR_DELAY_NORMAL);
		if (gSensor == null)
			gSensor = mSensor.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		mSensor.registerListener(this, gSensor, SensorManager.SENSOR_DELAY_GAME);
	}

	private void sensorRelease() {
		if (mSensor != null)
			mSensor.unregisterListener(this);
		mSensor = null;
		if (proximitySensor != null)
			proximitySensor = null;
		// if (gSensor != null);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		sensorRelease();
	}
	
	public void showGSensorInfo(double x,double y,double z){
		String msg = "GSensor info : \n"+
					  "   X : "+ x +" \n"+
					  "   Y : "+ y +" \n"+
					  "   Z : "+ z +" \n";
		gSensorInfo.setText(msg);
		/*if(x>0){
			images.setBackgroundResource(R.drawable.arrow_left);
		}else if(x<0){
			images.setBackgroundResource(R.drawable.arrow_right);
		}else if(y>0){
			images.setBackgroundResource(R.drawable.arrow_up);
		}else{
			images.setBackgroundResource(R.drawable.arrow_down);
		}*/
	}
	
	 private void showArrow(float x, float y) {
	        int arrowId = 0;

	        if (Math.abs(x) <= Math.abs(y)) {
	            if (y < 0) {
	                // up is low
	                arrowId = R.drawable.arrow_up;
	            } else if (y > 0) {
	                // down is low
	                arrowId = R.drawable.arrow_down;
	            } else if (y == 0) {
	                // do nothing
	            }
	        } else {
	            if (x < 0) {
	                // right is low
	                arrowId = R.drawable.arrow_right;
	            } else {
	                // left is low
	                arrowId = R.drawable.arrow_left;
	            }
	        }

	         images = (ImageView) findViewById(R.id.ls12_gsensor);
	        if (arrowId == 0) {
	        	images.setBackgroundDrawable(null);
	        } else {
	        	images.setBackgroundResource(arrowId);
	        }
	    }
}
